package org.fast.develop.logicflow.rest.execute;

import org.fast.develop.logicflow.core.meta.shape.LogicFlowDefinition;
import org.fast.develop.logicflow.management.service.LogicModelService;
import org.fast.develop.logicflow.runtime.LogicFlowRuntimeConfiguration;
import org.fast.develop.logicflow.runtime.LogicRuntimeService;
import org.fast.develop.logicflow.runtime.convert.execute.DesignerToDefinitionsExecutor;

import org.fast.develop.logicflow.runtime.debug.ExecuteDebugConfig;
import org.fast.develop.logicflow.runtime.instance.LogicFlowInstance;
import org.fast.develop.logicflow.ws.message.RunTimeMessage;

import java.util.Map;

/**
 * @author 莫寅
 * @createTime 2022-12-28 22:40
 * @desc：
 */
public class LogicFlowExecutorEnhance {

    private LogicRuntimeService logicRuntimeService;
    private LogicModelService logicModelService;
    private DesignerToDefinitionsExecutor definitionsToJsonExecutor;

    public LogicFlowExecutorEnhance(LogicFlowRuntimeConfiguration logicFlowRuntimeConfiguration, LogicModelService logicModelService) {
        this.logicRuntimeService = logicFlowRuntimeConfiguration.getLogicRuntimeService();
        this.logicModelService = logicModelService;
        this.definitionsToJsonExecutor = logicFlowRuntimeConfiguration.getLogicFlowConvertConfiguration().getDesignerToDefinitionsExecutor();
    }

    public LogicFlowInstance execute(String modelId, Map variables) {
        String schema = logicModelService.getSchema(modelId);

        LogicFlowDefinition logicFlowDefinition = definitionsToJsonExecutor.execute(schema);

        return logicRuntimeService.execute(logicFlowDefinition, variables, ExecuteDebugConfig.OfNull());
    }

    public LogicFlowInstance execute(String modelId, Map variables, ExecuteDebugConfig debugConfig) {
        String schema = logicModelService.getSchema(modelId);

        LogicFlowDefinition logicFlowDefinition = definitionsToJsonExecutor.execute(schema);

        return logicRuntimeService.execute(logicFlowDefinition, variables, debugConfig);
    }
}
